<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
require_once './Classes/PHPExcel.php';

//数据库连接
$db=new mysqli("localhost","root","root","shuad");


$objPHPExcel=new PHPExcel();
$objPHPExcel->getProperties()->setCreator('/')
        ->setLastModifiedBy('/')
        ->setTitle('Office 2007 XLSX Document')
        ->setSubject('Office 2007 XLSX Document')
        ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
        ->setKeywords('office 2007 openxml php')
        ->setCategory('Result file');
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','ID')
            ->setCellValue('B1','标题')
            ->setCellValue('C1','类型')
            ->setCellValue('D1','描述');

//查询语句

$ret=$db->query('SELECT * FROM `csad_production` ORDER BY id desc limit 100 ');

$ret->fetch_array();
$i=2;   
while ( $v=$ret->fetch_array()) {
   $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i,$v['id'])
            ->setCellValue('B'.$i,$v['name'])
            ->setCellValue('C'.$i,$v['tagid'])
            ->setCellValue('D'.$i,$v['descr']);
 $i++;
}
$objPHPExcel->getActiveSheet()->setTitle('用户信息');
$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('信息统计表').'_'.date('Y-m-dHis');



//*生成xlsx文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');


/*
*生成xls文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
*/

$objWriter->save('php://output');
exit;
?>